<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */

namespace MoChat\App\Call\Action\Dashboard;

use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Call\Constants\ClientTypeCode;
use MoChat\App\Call\Logic\RecordIndexLogic;
use MoChat\App\Client\Model\Client;
use MoChat\App\Common\Middleware\DashboardAuthMiddleware;
use MoChat\App\Rbac\Middleware\PermissionMiddleware;
use MoChat\App\Client\Logic\IndexLogic;
use MoChat\App\Sales\Contract\CallRecordContract;
use MoChat\App\Sales\Contract\SalesClientTaskContract;
use MoChat\App\Sales\Model\CallRecord;
use Hyperf\Di\Annotation\Inject;
use MoChat\App\User\Model\User;
use MoChat\App\Common\Middleware\SidebarAuthMiddleware;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;
/**
 * 呼叫回调处理
 * @Controller
 */
class CallBack extends AbstractAction
{
    /**
     * @Middlewares({
     * })
     * @RequestMapping(path="/dashboard/call/callBack", methods="GET,POST")
     */
    public function handle()
    {
        $re=$this->request->all();
        @file_put_contents('1.txt',json_encode($re,JSON_UNESCAPED_UNICODE+JSON_UNESCAPED_SLASHES).PHP_EOL,FILE_APPEND);
//        $extn=Db::table('user')->where('phone',$re["exte"])->value('extn');
        $body=empty($re['memo'])?[]:json_decode($re['memo'],true);
        $call_type="in";
        if ($body['cdrPeer']==1 || $body['cdrPeer']==3){
            $call_type="out";
        }
        if ($body['cdrPeer']==2){
            $call_type="in";
        }
        Db::table('call_record')->insert([
            "callaccept" => $body["cdrId"]??"",
            "caller_id_name" => $body["cdrCallinName"]??"",
            "caller_id_number" => $body["cdrCallinPhone"]??"",
            "destination_number" => $body["cdrPhoneNum"]??null,
            "created_time" => $body["cdrStartTime"]??null,
            "answered_time" => $body["cdrSucessTime"]??"",
            "ivr_dtmf" => $body["cdrIVRKey"]??"",
            "business_type" => "",
            "over_time" => $body["cdrHangupTime"]??null,
            "status" => $body["cdrReadState"]??0,
            "gateway" => $body["cdrTrunkRoute"]??"",
            "batch_accept" => "",
            "reday" => null,
            "hangup_side" => $body["cdrHangupReasonText"]??"",
            "year" => null,
            "month" => null,
            "record_url" => "https://voip.800ing.com/voip/api/client/download/record.json?id=".$body['cdrId']."&ismp3=1",
            "call_type" => $call_type,
            "wait_duration" =>isset($body["cdrSucessTime"]) && isset($body['cdrStartTime'])? strtotime($body["cdrSucessTime"])-strtotime($body['cdrStartTime']):0,
            "call_duration" => $body["cdrTalktime"]??0,
            "other_str" => "",
            "trans_number" => "",
            "other_accept" => $body["cdrId"]??"",
            "deal_result" => "",
            "da_similarity" =>"",
            "da_ar" => "",
            "da_type" => "",
            "queue_uuid" => "",
            "user_uuid" =>"",
            "dtmf" => "",
            "type"=>2,
        ]);
        return [];
    }

}
